package org.ovhoo.zonfig;

import org.ovhoo.io.ZBufferedWriter;


/**
 * Represent a zconfig entry element
 * @author mzalim
 *
 */
public class Entry {
	//attributes
	
	private String name;
	private Type.typesEnum type;
	private Value value;
	private String stringType;
	
	
	/**
	 * constructor with a specified name
	 * @param name : The  name
	 * @param type : the entry type
	 */
	public Entry(String name, Type.typesEnum type){
		this.name = name;
		this.type = type;
		this.stringType = Type.typeToString(type);
	}
	
	/**
	 * constructor with a specified name
	 * @param name : The  name
	 * @param type : the entry type
	 */
	public Entry(String name, String type){
		this.name = name;
		this.stringType = type;
		this.type = Type.stringToType(type);
	}
	
	
	/**
	 * returns the name
	 * @return The name
	 */
	public String getName(){
		return this.name;
	}
	
	/**
	 * Change the value of the attribute name
	 * @param name : the value of the attribute name
	 */
	public void setName(String name){
		this.name = name;
	}
	
	/**
	 * returns the type
	 * @return The type
	 */
	public Type.typesEnum getType(){
		return this.type;
	}
	
	/**
	 * return the type as a string
	 * @return the string value of the entry type
	 */
	public String getStringType(){
		if (this.type != Type.typesEnum.specified){
			return Type.typeToString(this.type);
		}
		else{
			return this.stringType;
		}
	}
	
	/**
	 * Change the value of the attribute type
	 * @param type : the value of the attribute type
	 */
	public void setType(Type.typesEnum type){
		this.type = type;
		this.stringType = Type.typeToString(type);
	}
	
	/**
	 * Change the value of the attribute type
	 * @param type : the value of the attribute type
	 */
	public void setType(String type){
		this.stringType = type;
		this.type = Type.stringToType(type);
	}
	
	/**
	 * Affect the value of the object
	 * @param value
	 */
	public void setValue(String value){
		if (value != null){
			this.value = new Value(this.type, value);
		}
			
	}
	
	/**
	 * return the value of the entry
	 * @return the value of the entry
	 */
	public Value getValue(){
		return this.value;
	}
	
	/**
	 * write the configuration file
	 * @param bufferWriter : buffer writer
	 * @param prefix : the prefix
	 */
	public void writeContent(ZBufferedWriter bufferWriter, String prefix) {
		bufferWriter.write(prefix + "<entry name=\"" + this.name +"\" type=\"" + this.getStringType() + "\"");
		
		if (this.value != null){
			bufferWriter.writeln(" value=\"" + this.value +"\"/>");
		}
		else{
			bufferWriter.writeln(" />");
		}
	}
	

}
